home *** CD-ROM | disk | FTP | other *** search
- /*
- uudecode 36.6 -- Copyright (c) 1993 Ralph Seichter.
-
- Decodes binary files encoded with uuencode.
-
- This tool is FREELY DISTRIBUTABLE. Copying and spreading is encouraged,
- as long as you don't charge anyone for giving him/her this tool. Please
- note that the Copyright is mine, this is *NOT* public domain software!
- Permission is hereby granted to include the complete (!) archive in all
- non-profit public domain software series like Fred Fish, SaarAG, etc.
-
- This version of uuencode was especially optimized for AmigaDOS 2.04 or
- better and the SAS/C 6.3 development system.
- */
-
- #include <proto/dos.h>
- #include <proto/exec.h>
- #include <proto/utility.h>
- #include <exec/memory.h>
-
- #define ARG_FROM 0
- #define ARG_PATH 1
- #define ARG_NOCHKSUM 2
- #define ARG_TOTAL 3
- #define LINESIZE 128
- #define SIXBIT 0x40 /* 6 bit = 0x40 */
-
- /* decode one single byte */
-
- #define DECODE(c) ((c - 0x20) & 0x3F)
-
-
-
- /* function prototypes and globals ******************************************/
-
- LONG atol (STRPTR);
- LONG decodeFile (struct Library *, BPTR, BOOL);
- BOOL putFile (BPTR, STRPTR, ...);
- BOOL putStr (STRPTR, ...);
- LONG uudecode (VOID);
-
- UBYTE ver[] = "$VER: uudecode 36.6 (24.8.93) Copyright \xA9 1993 Ralph Seichter";
- struct DosLibrary *DOSBase;
-
-
-
- /* the main program *********************************************************/
-
- LONG __saveds uudecode (VOID)
- {
- LONG rc = RETURN_FAIL;
-
- /* uudecode will *NOT* run without dos.library V36 or better! */
- if (DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 36))
- {
- struct Library *UtilityBase;
- LONG err = 0;
-
- if (UtilityBase = OpenLibrary ("utility.library", 37))
- {
- STRPTR arg[ARG_TOTAL] = {0, 0, 0};
- struct RDArgs *rda;
-
- if (rda = ReadArgs ("FROM/A/M,PATH/K,NOCHECKSUM/S", (LONG *)arg, NULL))
- {
- LONG i;
- BPTR in;
- STRPTR *name = (STRPTR *)arg[ARG_FROM];
-
- for (err = 0, i = 0; err == 0 && (name[i]); ++i)
- {
- if (in = Open (name[i], MODE_OLDFILE))
- {
- BPTR oldPath, path = NULL;
-
- if ((arg[ARG_PATH]) && (path = Lock (arg[ARG_PATH], SHARED_LOCK)))
- oldPath = CurrentDir (path);
-
- putStr ("\nProcessing input file `%s'\n", name[i]);
- err = decodeFile (UtilityBase, in, (BOOL)arg[ARG_NOCHKSUM]);
-
- if (path)
- {
- CurrentDir (oldPath);
- UnLock (path);
- }
- Close (in);
- }
- }
- rc = (err == 0 ? RETURN_OK : RETURN_ERROR);
- FreeArgs (rda);
- }
- CloseLibrary (UtilityBase);
- }
- if (err > 0 || (err = IoErr ()) > 0)
- PrintFault (err, NULL);
- CloseLibrary ((struct Library *)DOSBase);
- }
- return (rc);
- }
-
-
-
- /* ASCII to LONG ************************************************************/
-
- LONG atol (STRPTR s)
- {
- LONG x = 0;
-
- while (*s >= '0' && *s <= '9')
- x = 10 * x + (LONG)(*s++ - '0');
- return (x);
- }
-
-
-
- /* sort of a private fprintf() function *************************************/
-
- BOOL putFile (BPTR file, STRPTR format, ...)
- {
- UBYTE buf[256];
-
- RawDoFmt (format, &format + 1, (void (*))"\x16\xC0\x4E\x75", buf);
- return ((BOOL)(0 == FPuts (file, buf)));
- }
-
-
-
- /* sort of a private printf() function *************************************/
-
- BOOL putStr (STRPTR format, ...)
- {
- UBYTE buf[256];
-
- RawDoFmt (format, &format + 1, (void (*))"\x16\xC0\x4E\x75", buf);
- return ((BOOL)(0 == PutStr (buf)));
- }
-
-
-
- /* decode a complete file ***************************************************/
-
- LONG decodeFile (struct Library *UtilityBase, BPTR in, BOOL ignoreChecksum)
- {
- BOOL done, gotBegin = FALSE;
- LONG line = 0;
- UBYTE __aligned buf[LINESIZE];
- UBYTE __aligned outbuf[LINESIZE];
- UBYTE error[] = "\7\23331;42m ERROR \2330m -- ";
- UBYTE warning[] = "\23331;42m WARNING \2330m -- ";
- STRPTR s, t, empty;
-
- if (empty = AllocVec (LINESIZE, MEMF_CLEAR))
- {
- do
- {
- done = TRUE;
-
- /* find the next `begin' line */
- while (s = FGets (in, buf, LINESIZE - 1))
- {
- ++line;
- if (0 == Strnicmp (buf, "begin ", 6))
- {
- gotBegin = TRUE;
-
- /* skip over `begin xyz ' to find the file name. */
- t = s = FilePart (&buf[10]);
- while (*t != '\n')
- ++t;
- *t = 0;
- break;
- }
- }
-
- /* is there a file to decode? */
- if (s)
- {
- BPTR out;
-
- if (out = Open (s, MODE_NEWFILE))
- {
- LONG expected = ~0, size = 0;
-
- putStr ("\nLine %ld: decoding file `%s'\n", line, s);
- CopyMemQuick (empty, buf, LINESIZE);
- while (s = FGets (in, buf, LINESIZE - 1))
- {
- ++line;
- if (0 == Stricmp (buf, "end\n"))
- {
- if (FGets (in, buf, LINESIZE - 1))
- {
- ++line;
- if (0 == Strnicmp (buf, "size ", 5))
- expected = atol (&buf[5]);
- }
- break;
- }
- else
- {
- LONG l, c, checksum = 0;
-
- t = outbuf;
- c = *s++;
- l = DECODE(c);
- if (c != '\n' && l > 0)
- {
- /* decode one line. four input bytes will
- result in three output bytes. */
- while (l >= 4)
- {
- c = DECODE(s[0]) << 2 | DECODE(s[1]) >> 4;
- checksum += c;
- *t++ = c;
-
- c = DECODE(s[1]) << 4 | DECODE(s[2]) >> 2;
- checksum += c;
- *t++ = c;
-
- c = DECODE(s[2]) << 6 | DECODE(s[3]);
- checksum += c;
- *t++ = c;
-
- checksum = checksum % SIXBIT;
- s += 4;
- l -= 3;
- }
- c = DECODE(s[0]) << 2 | DECODE(s[1]) >> 4;
- checksum += c;
- if (l >= 1)
- *t++ = c;
- c = DECODE(s[1]) << 4 | DECODE(s[2]) >> 2;
- checksum += c;
- if (l >= 2)
- *t++ = c;
- c = DECODE(s[2]) << 6 | DECODE(s[3]);
- checksum += c;
- if (l >= 3)
- *t++ = c;
- checksum = checksum % SIXBIT;
- s += 4;
- if (!ignoreChecksum && *s >= 0x20 && *s <= 0x60 && checksum != DECODE(*s))
- {
- putStr ("Line %ld: %sbad checksum!\n", line, error);
- break;
- }
- l = t - outbuf;
- if (1 == FWrite (out, outbuf, l, 1))
- size += l;
- else
- break;
- }
- }
- }
- if (s)
- {
- if (expected != ~0 && expected != size)
- putStr ("Line %ld: %sfile size mismatch!\n%ld bytes expected, ", line, warning, expected);
- putStr ("%ld bytes written.\n", size);
- done = FALSE;
- }
- else
- putStr ("Line %ld: %sunexpected end of file!\n", line, error);
- Close (out);
- }
- }
- }
- while (!done);
- if (!gotBegin)
- putStr ("%sno `begin' line found.\n", warning);
- FreeVec (empty);
- }
- return (IoErr ());
- }
-